package com.example.ice.u17.fragment;


import android.content.Context;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.ice.u17.R;
import com.example.ice.u17.adapter.RecAdapter;
import com.example.ice.u17.entries.RecommendBean;
import com.example.ice.u17.other_activity.Classify_Activity;
import com.example.ice.u17.util.ClassType;
import com.example.ice.u17.util.NetUtils;
import com.example.ice.u17.util.PullToRefresh;
import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class RecommendFragment extends Fragment implements Callback<RecommendBean>, View.OnClickListener {

    private PullToRefresh rec_ptr;
    private RecAdapter recAdapter;
    private int pager = 1;
    private ImageView rec_search;
    private boolean isHasMore;
    private boolean isLoadingMore=false;
    private LinearLayoutManager mLayoutManager;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_recommend, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        rec_ptr = ((PullToRefresh) view.findViewById(R.id.rec_PullToRefresh));
        rec_search = ((ImageView) view.findViewById(R.id.rec_search));
        rec_search.setOnClickListener(this);
        recAdapter = new RecAdapter(getContext());
        final RecyclerView refreshableView = rec_ptr.getRefreshableView();
        mLayoutManager = new LinearLayoutManager(getContext());
        refreshableView.setLayoutManager(mLayoutManager);
        refreshableView.setAdapter(recAdapter);
        NetUtils.getData(ClassType.RecommendBean, this, pager);
//        rec_ptr.setScrollingWhileRefreshingEnabled(true);
        refreshableView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(final RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                int lastVisibleItem = ((LinearLayoutManager) mLayoutManager).findLastVisibleItemPosition();
                int totalItemCount = mLayoutManager.getItemCount();
                //lastVisibleItem >= totalItemCount - 4 表示剩下4个item自动加载，各位自由选择
                // dy>0 表示向下滑动
                if (lastVisibleItem >= totalItemCount - 5 && dy > 0) {
                    if(isLoadingMore&&isHasMore){

                    } else{
                        NetUtils.getData(ClassType.RecommendBean, new Callback<RecommendBean>() {
                            @Override
                            public void onResponse(Call<RecommendBean> call, Response<RecommendBean> response) {
                                RecommendBean.DataBean.ReturnDataBean returnData = response.body().getData().getReturnData();
                                isHasMore = returnData.isHasMore();
                                recAdapter.addAll(returnData.getDataList());
                                isLoadingMore = false;
                            }

                            @Override
                            public void onFailure(Call<RecommendBean> call, Throwable t) {
                                Snackbar.make(recyclerView, "数据加载失败", Snackbar.LENGTH_SHORT);
                            }
                        }, ++pager);
                        isLoadingMore = true;
                    }
                }
            }
        });
        rec_ptr.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<RecyclerView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<RecyclerView> pullToRefreshBase) {
//                ILoadingLayout loadingLayoutProxy = rec_ptr.getLoadingLayoutProxy(true, false);
//                loadingLayoutProxy.setPullLabel("有妖气！！！");
//                loadingLayoutProxy.setLoadingDrawable(getContext().getResources().getDrawable(R.mipmap.u17_refresh_eye_nictation2));
                rec_ptr.setRefreshing(true);
                recAdapter.removeAll();
                NetUtils.getData(ClassType.RecommendBean, RecommendFragment.this, 1);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<RecyclerView> pullToRefreshBase) {
                rec_ptr.onRefreshComplete();
//                ILoadingLayout loadingLayoutProxy = rec_ptr.getLoadingLayoutProxy(false, true);
//                loadingLayoutProxy.setRefreshingLabel(null);
//                loadingLayoutProxy.setPullLabel(null);
//                loadingLayoutProxy.setLoadingDrawable(null);
//                if (isHasMore) {
//                    rec_ptr.setRefreshing(true);
//                    NetUtils.getData(ClassType.RecommendBean, RecommendFragment.this, ++pager);
//                } else {
//                    rec_ptr.onRefreshComplete();
//                    loadingLayoutProxy.setPullLabel("队长大人！！到底啦！！！");
//                    loadingLayoutProxy.setLoadingDrawable(getContext().getResources().getDrawable(R.mipmap.main_reminder_bottom));
//                }
            }
        });
    }

    @Override
    public void onResponse(Call<RecommendBean> call, Response<RecommendBean> response) {
        RecommendBean.DataBean.ReturnDataBean returnData = response.body().getData().getReturnData();
        isHasMore = returnData.isHasMore();
        recAdapter.addAll(returnData.getDataList());
        rec_ptr.onRefreshComplete();
    }

    @Override
    public void onFailure(Call<RecommendBean> call, Throwable t) {
        Snackbar.make(rec_ptr, "获取数据失败", Snackbar.LENGTH_SHORT).show();
        rec_ptr.onRefreshComplete();
    }

    @Override
    public void onClick(View v) {
        // TODO: 2016/9/30  跳转页面
        Intent intent=new Intent(getContext(), Classify_Activity.class);
        startActivity(intent);
    }
}
